package com.jubotech.business.web.service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jubotech.business.web.dao.CommonStateDao;
import com.jubotech.business.web.dao.SysAutoSettingDao;
import com.jubotech.business.web.domain.CommonState;
import com.jubotech.business.web.domain.CustomerInfo;
import com.jubotech.business.web.domain.SysAutoSetting;
import com.jubotech.business.web.domain.WxAccountInfo;
import com.jubotech.business.web.query.CommonStateQuery;
import com.jubotech.framework.common.ResultInfo;
import com.jubotech.framework.common.ServiceException;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional // 支持事务
public class CommonStateService {

	@Resource
	private CommonStateDao commonStateDao;

	@Autowired
	private SysAutoSettingDao sysAutoSettingDao;

	@Resource
	private WxAccountService weChatAccountService;

	public PageInfo<CommonState> pageList(CommonStateQuery query) {
		PageHelper.startPage(query.getPage(), query.getRows());

		Example example = new Example(CommonState.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("adminId", query.getAdminId());
		if (null != query.getState()) {
			criteria.andEqualTo("state", query.getState());
		}
		if (null != query.getType()) {
			criteria.andEqualTo("type", query.getType());
		}
		example.orderBy("id").desc();
		return new PageInfo<>(commonStateDao.selectByExample(example));

	}

	public int deleteByIds(String ids) {
		if (StringUtils.isBlank(ids)) {
			throw new ServiceException("invalid param");
		}
		String[] idArray = StringUtils.split(ids, ",");
		Set<String> userIds = Arrays.stream(idArray).filter(StringUtils::isNotBlank).collect(Collectors.toSet());
		Example example = new Example(CustomerInfo.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("id", userIds);

		if (null != userIds && userIds.size() > 0) {
			for (String str : userIds) {
				Integer id = Integer.valueOf(str);
				CommonState commonState = commonStateDao.selectByPrimaryKey(id);
				if (null != commonState) {
					SysAutoSetting sys = sysAutoSettingDao.findSettingByCidType(commonState.getAdminId(),commonState.getType());
					if (null != sys) {
						sysAutoSettingDao.delete(sys);
					}
				}
			}

		}

		return commonStateDao.deleteByExample(example);
	}

	public CommonState getCommonStateByid(Integer id) {
		CommonState user = commonStateDao.selectByPrimaryKey(id);
		return user;
	}
	
	public CommonState queryCommonStateByCid(Integer cid,Integer type) {
		try {
			Example example = new Example(CommonState.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("adminId", cid);
			criteria.andEqualTo("type", type);
			return  commonStateDao.selectOneByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public ResultInfo insert(CommonState info) {
		ResultInfo res = new ResultInfo();
		try {
			CommonState commonState = queryCommonStateByCid(info.getAdminId(), info.getType());
			if(null != commonState) {
				return ResultInfo.fail("类型已存在不能重复添加");
			}
			
			info.setCreateTime(new Date());
			commonStateDao.insert(info);

			// 维护自动功能
			saveOrUpdateAutoSetting(info);

		} catch (Exception e) {
			return ResultInfo.fail("添加失败");
		}
		return res;
	}

	public ResultInfo update(CommonState info) {
		ResultInfo res = new ResultInfo();
		try {
			info.setCreateTime(new Date());
			commonStateDao.updateByPrimaryKey(info);

			// 维护自动功能
			saveOrUpdateAutoSetting(info);
		} catch (Exception e) {
			return ResultInfo.fail("修改失败");
		}
		return res;
	}

	public List<CommonState> listByAdminId(Long adminId) {
		Example example = new Example(CommonState.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("adminId", adminId);
		return commonStateDao.selectByExample(example);
	}

	private void saveOrUpdateAutoSetting(CommonState info) {
		Integer cid = info.getAdminId();
		List<WxAccountInfo> wechatList = weChatAccountService.findWeChatAccountInfoByCid(cid);
		if (null != wechatList && wechatList.size() > 0) {
			for (int i = 0; i < wechatList.size(); i++) {
				WxAccountInfo wechat = wechatList.get(i);
				SysAutoSetting sas = new SysAutoSetting(cid, info.getType(), wechat.getWechatid(), info.getState(),
						new Date());
				SysAutoSetting sys = sysAutoSettingDao.findSysAutoSettingByWeChatId(cid, info.getType(),
						wechat.getWechatid());
				if (null == sys) {
					sysAutoSettingDao.insert(sas);
				} else {
					sysAutoSettingDao.delete(sys);
					sysAutoSettingDao.insert(sas);
				}

			}
		}
	}

}
